import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;


public abstract class Parser 
{	
	// Get page content
	public static BufferedReader get(String url)
	{
		URLConnection  connection  = null;
		BufferedReader reader      = null;
		
		try
		{
			connection = new URL(url).openConnection();
			reader     = new BufferedReader(new InputStreamReader(connection.getInputStream()));
		}
		catch (MalformedURLException e)
		{
			//System.out.println("Error: Malformed URL");
			e.printStackTrace();
		}
		catch (IOException e) {
			//System.out.println("Error: Input/Output error");
			e.printStackTrace();
		}
		
		return reader;
	}
	
	// Parse an html page and add it into database
	public static int parse(Database d, String url, String family, String species, int cpt_arn) throws IOException
	{
		BufferedReader reader = null;
		String  content = "";
		boolean read    = false;
		int     cpt     = 5;

		Arn current_arn = new Arn();
		// Page reachable
		if ((reader = get(url)) != null)
		{
			while ((content = reader.readLine()) != null)
			{
				content = content.replaceAll("[ \t]+", " ");
				
				// Parsing starts only after <hr> tag
				if (content.contains("<HR>"))  { read = true; }
				// Each tRNA is 4 lines after <pre> tag
				if (content.contains("<PRE>"))
				{
					cpt = 0;
				}
				
				current_arn.setFamily(family);
				current_arn.setSpecies(species);
				
				// Parsing first line
				if (cpt == 1)
				{
					if(content.contains("Length"))
					{
						String[] currTab1 = content.split(" ");
						
						current_arn.setId(currTab1[0]);
						current_arn.setLength(currTab1[3]);
					}
					else
					{
						cpt--;
					}
				}
				
				// Parsing second line
				if (cpt == 2)
				{
					if(content.contains("Type"))
					{
						
						String[] currTab2 = content.split(" ");
						
						current_arn.setType(currTab2[1]);
						
						current_arn.setAnticodon(currTab2[3]);
						
						String[] positionTab = currTab2[5].split("-");
						current_arn.setPosition(positionTab[0]);
						
						current_arn.setScore(currTab2[8]);				
					}
					else
					{
						cpt--;
					}
				}
				
				// Parsing third line
				if (cpt == 3)
				{
					if(content.contains("Seq"))
					{
						String[] currTab3 = content.split(" ");
						current_arn.setSeq(currTab3[1].toUpperCase());
					}
					else
					{
						cpt--;
					}
				}
				
				// Parsing fourth and last line
				if (cpt == 4)
				{
					if(content.contains("Str"))
					{
						String[] currTab4 = content.split(" ");
						current_arn.setStr(currTab4[1]);
						
						
						
						// Parsing for this tARN ended, let's add it to the database !
						if (current_arn != null)
						{
							if (current_arn.valid())
							{
								d.add(current_arn);
								cpt_arn++;
								//System.out.println("?" + current_arn.toString());
								current_arn = new Arn();
							}
							else
							{
								d.error();
							}
						}
					}
					else
					{
						cpt--;
					}
				}			
								
				// Continue reading ...
				if (read) { cpt++; }
			}
		}
		return cpt_arn;
		//System.out.println(d.db.toString());	
	}
}
